package gw.com.android.ui.dialog;

import android.animation.AnimatorSet;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppMain;
import gw.com.android.presenter.NoviceWelfarePresenter;
import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2018-09-07.
 */
public class NoviceWelfarePopup {

    private final PopupWindow mPopup;
    private final View anchor;
    private final View rootViw;

    public NoviceWelfarePopup(Activity activity, View anchor, final String title, final String link) {
        this.anchor = anchor;
        rootViw = createView(activity, title, link);
        mPopup = new PopupWindow(rootViw, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopup.setFocusable(false);
        mPopup.setOutsideTouchable(false);
        mPopup.setAnimationStyle(R.style.popp_anim);
    }

    private View createView(Activity activity, final String title, final String link) {
        return NoviceWelfarePresenter.getInstance().createView(activity, title, link);
    }

    public void show() {
        if (mPopup != null) {
            int top = DeviceUtil.instance().dip2px(130, AppMain.getApp());
            mPopup.showAtLocation(anchor, Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, top);
        }
    }

    public void hide() {
        if (rootViw != null) {
            AnimatorSet set = (AnimatorSet) rootViw.getTag();
            set.cancel();
        }
        if (mPopup != null && mPopup.isShowing()) {
            mPopup.dismiss();
        }
    }

}
